<!-- Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<link rel="import" href="../common/cr-action.html">
<link rel="import" href="../diff/cr-diff.html">

<polymer-element name="cr-issue-patchfile" attributes="file mode active">
    <template>
        <link rel="stylesheet" href="../common/common.css">
        <link rel="stylesheet" href="cr-issue-patchfile.css">

        <div class="file">
            <div class="file-status">{{ file.status }}</div>
            <div class="file-removed">-{{ file.removed }}</div>
            <div class="file-added">+{{ file.added }}</div>
            <div class="file-name"><a is="cr-action" on-tap="{{ toggleDiff }}" href="{{ singleFileUrl }}" target="_blank">{{ file.name }}</a></div>
            <div class="file-messages">
                {{ file.messageCount - file.draftCount | pluralize("comment") }}
                <template if="{{ file.draftCount }}">
                    <template if="{{ file.messageCount - file.draftCount }}">,</template>
                    {{ file.draftCount | pluralize("draft") }}
                </template>
            </div>
        </div>

        <cr-diff id="diff" file="{{ file }}" mode="{{ mode }}" active="{{ active }}"></cr-diff>
    </template>
    <script>
        Polymer({
            created: function() {
                this.file = null;
                this.mode = "unified";
                this.active = false;
                this.singleFileUrl = "";
            },
            showDiff: function(mode) {
                return this.$.diff.showDiff();
            },
            hideDiff: function() {
                return this.$.diff.hideDiff();
            },
            toggleDiff: function(event) {
                if (event && (event.metaKey || event.ctrlKey))
                    return;
                return this.$.diff.toggleDiff();
            },
            pluralize: function(count, text) {
                if (!count)
                    return "";
                if (count == 1)
                    return count + " " + text;
                return count + " " + text.pluralize();
            },
            fileChanged: function(oldValue, newValue) {
                if (newValue)
                    this.singleFileUrl = newValue.getSingleViewUrl();
            },
        });
    </script>
</polymer-element>
